package com.lagou.homework.stage02.module01.Task01.app;

import com.lagou.homework.stage02.module01.Task01.entity.Account;
import com.lagou.homework.stage02.module01.Task01.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TraTest {
    public static void main(String[] args) {
        Connection con = DruidUtils.getConnection();
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql01 = "select * from account where card = ?";
            Account acc01 = qr.query(sql01, new BeanHandler<Account>(Account.class), "1122334455");
            System.out.println(acc01);
            if(5000>acc01.getBalance()){
                System.out.println("余额不足！");
                return;
            }

            String sql02 = "update account set balance = balance - 5000 where card = 1122334455";
            qr.update(sql02);

            String sql03 = "update account set balance = balance + 5000 where card = 55443332211";
            qr.update(sql03);

            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            dateFormat.format(date);
            String sql04 = "insert into transaction values(?,?,?,?,?)";
            Object[] parm01={null,"1122334455","转出",5000,date};
            Object[] parm02={null,"5544332211","转入",5000,date};
            qr.update(con,sql04,parm01);
            qr.update(con,sql04,parm02);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
                DruidUtils.close(con);
        }

    }
}
